package main

import (
	"context"
	kafka "github.com/segmentio/kafka-go"
)

// 定义kafka的结构体，用于接收kafka的消息，包含tcp地址，主题，分区
type KafkaClient struct {
	Addr      string
	Topic     string
	Partition int
}

func main() {
	// 初始化kafka结构体
	kafkac := KafkaClient{
		Addr:      "192.168.152.133:9092",
		Topic:     "my_topic",
		Partition: 0,
	}
	// 初始化kafka的连接
	conn, err := kafka.DialLeader(context.Background(), "tcp", kafkac.Addr, kafkac.Topic, kafkac.Partition)
	if err != nil {
		panic(err)
	}

	// 发送消息
	_, err = conn.WriteMessages(
		kafka.Message{
			Key:   []byte("Key-A"),
			Value: []byte("one!")},
	)
	if err != nil {
		panic(err)
	}
}
